<template>
  <div class="goodsConfig h100" v-loading="loading">
    <div class="pageContent">
      <el-form ref="ruleForm" class="form" :model="ruleForm" :inline="true" label-width="80px">
        <el-row :gutter="11" class="fileSearch">
          <!-- <el-form-item label="手机号:" prop="loginPhone">
            <el-input v-model="ruleForm.loginPhone" clearable></el-input>
          </el-form-item>
          <el-form-item label="姓名:" prop="fullName">
            <el-input v-model="ruleForm.fullName" clearable></el-input>
          </el-form-item> -->
          <el-form-item label="商品类型" prop="categoryCode">
            <el-select v-model="ruleForm.categoryCode" placeholder="请选择商品类型">
              <el-option label="全部" value=""></el-option>
              <el-option label="代为祭扫" value="sweep"></el-option>
              <el-option label="供奉商品" value="article"></el-option>
              <el-option label="祭祀的灯" value="lantern"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="onSubmit(1)">
              查询
            </el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            <el-button icon="el-icon-plus" type="success" @click="dialogVisible = true"
              >新增</el-button
            >
          </el-form-item>
        </el-row>
        <div class="table_wrapper">
          <el-table
          :data="tableData"
          border
          :header-cell-style="{
            background: 'rgb(246,247,251)',
            color: '#606266',
          }"
          stripe
          class="defaultTableHeight"
        >
          <el-table-column type="index" label="编号" width="50"> </el-table-column>
          <el-table-column prop="parentTitle" align="center" label="类型">
          </el-table-column>
          <el-table-column prop="title" align="center" label="商品类型">
          </el-table-column>

          <!-- <el-table-column prop="asLogin" align="center" label="禁用启用">
            <template slot-scope="scope">
              <el-button
                :type="scope.row.asLogin === 0 ? 'danger' : 'success'"
                size="mini"
                >{{ scope.row.asLogin === 0 ? "禁用" : "启用" }}
              </el-button>
            </template>
          </el-table-column> -->
          <el-table-column label="操作" align="center" width="300px">
            <template slot-scope="scope">
              <!-- <el-button type="primary" size="mini">编辑</el-button> -->
              <el-button type="danger" size="mini" @click="delGoods(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <p style="text-align: right;">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pager.currentPage"
            :page-size="pager.pageSize"
            :page-sizes="[10, 20, 30, 50]"
            background
            layout="total,sizes,prev, pager, next, jumper"
            :total="pager.total"
          >
          </el-pagination>
        </p>
        </div>
      </el-form>
    </div>
    <el-dialog
      title="添加商品类型"
      :visible.sync="dialogVisible"
      width="600px"
      v-dialogDrag
      @close="closeDialog"
      @opened="opened"
    >
      <el-form
        ref="goodsForm"
        :model="goodsForm"
        :rules="goodsFormRules"
        label-width="80px"
      >
        <el-form-item label="商品类型" prop="categoryCode">
          <el-select v-model="goodsForm.categoryCode" placeholder="请选择商品类型">
            <el-option label="代为祭扫" value="sweep" selected></el-option>
            <el-option label="供奉商品" value="article"></el-option>
            <el-option label="祭祀的灯" value="lantern"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型" prop="title">
          <el-input ref="inputTitle" v-model="goodsForm.title"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="saveGoods">保存</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// import * as appUser from "@/api/appUser";
import {
  category_loadList,
  category_operation,
  category_delete,
} from "@/api/adminConfig/goodsTypeConfig";
export default {
  name: "goodsTypeConfig",
  data() {
    return {
      loading: false,
      dialogVisible: false,
      ruleForm: {
        categoryCode: "",
      },
      goodsForm: {
        categoryCode: "sweep",
        title: "",
      },
      pager: {
        pageSize: 20,
        total: 1,
        currentPage: 1,
      },
      tableData: [],
      goodsFormRules: {
        title: [{ required: true, message: "请输入商品类型", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.onSubmit();
  },
  //注销window.onresize事件
  destroyed() {},
  methods: {
    async onSubmit(val) {
      if (val) {
        this.pager.currentPage = 1;
      }
      this.loading = true;
      let postData = {
        categoryCode: this.ruleForm.categoryCode,
        pageSize: this.pager.pageSize,
        total: this.pager.total,
        currentPage: this.pager.currentPage,
      };
      try {
        const res = await category_loadList(postData);
        console.log(res, 200);
        this.loading = false;
        if (res.code !== "2000") {
          this.loading = false;
          this.$message(res.msg ? res.msg : "数据读取失败");
          return;
        }

        this.tableData = res.data;
        this.pager.pageSize = res.pageSize;
        this.pager.total = res.total;
        this.pager.currentPage = res.currentPage;
        //数据读取成功
      } catch (err) {
        this.$message.error(err.msg);
        console.log(err, 404);
        this.loading = false;
        this.tableData = [];
      }
    },

    // 保存
    async saveGoods() {
      this.$refs["goodsForm"].validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let postData = this.goodsForm;
          console.log(postData, "postData");
          try {
            const res = await category_operation(postData);
            console.log(res, 200);
            this.loading = false;
            if (res.code !== "2000") {
              this.$message.error(res.msg ? res.msg : "操作失败");
              return;
            }
            this.$message.success("操作成功");
            this.onSubmit();
            this.closeDialog();
          } catch (err) {
            this.loading = false;
            console.log(err, 404);
            this.$message.error(err.msg);
          }
        }
      });
    },
    delGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let postData = { ids: [id] };
          console.log(postData, "postData");
          try {
            const res = await category_delete(postData);
            console.log(res, 200);
            if (res.code !== "2000") {
              this.$message.error(res.msg ? res.msg : "删除失败");
              return;
            }
            this.$message.success("删除成功");
            this.onSubmit();
          } catch (err) {
            this.$message.error(err.msg);
            console.log(err, 404);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    opened() {
      this.$refs.inputTitle.focus();
    },
    // 关闭弹窗的回调
    closeDialog() {
      this.$refs["goodsForm"].resetFields();
      this.dialogVisible = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["ruleForm"].resetFields();
      // this.onSubmit();
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pager.pageSize = size;
      this.onSubmit();
    },
    handleCurrentChange: function (currentPage) {
      this.pager.currentPage = currentPage;
      this.onSubmit(); //点击第几页
    },
  },
};
</script>

<style lang="scss" scoped>
.el-button {
  margin-left: 20px;
}
</style>
